<?php
    header('content-type:text/html;charset="utf-8"');
    $responseData = array("code" => 0,"message" => "");
    $username = $_POST['username'];

    $link = mysqli_connect("localhost","root","root");

    if(!$link){
        $responseData['code'] = 1;
        $responseData['message'] = "数据库连接失败";
        // 返回到前台页面
        echo json_encode($responseData);
        exit; //结束后续操作
    }

    //3.设置字符集
    mysqli_set_charset($link,"utf8");

    //4.选择数据库
    mysqli_select_db($link,"租售系统");

    $sql = "SELECT * FROM buy WHERE username='{$username}'";

    $res = mysqli_query($link,$sql);

    $arr = array();
    //7.处理结果集 
    if($res){
        while($row = mysqli_fetch_assoc($res)){
            array_push($arr,$row);
        }
        echo json_encode($arr);       
    }
    
    mysqli_close($link);
?>